//
//  PDColoredProgressView.m
//
//  Created by Pascal Widdershoven on 03-01-09.
//  Copyright 2009 Pascal Widdershoven. All rights reserved.
//  

#import "UIColoredProgressView.h"
#import "QuartzUtil.h"


@implementation UIColoredProgressView

@synthesize cornerRadius = _cornerRadius;
@synthesize borderColor = _borderColor;
@synthesize bLinearGradient = _bLinearGradient;
@synthesize bBorder = _bBorder;
@synthesize bInset = _bInset;
@synthesize bAroundRect = _bAroundRect;

@synthesize progRadius = _progRadius;
@synthesize progInset = _progInset;

- (id) initWithCoder: (NSCoder*)aDecoder
{
	if(self=[super initWithCoder: aDecoder])
	{
		_cornerRadius = 5;
		_bLinearGradient = YES;
		_bBorder = YES;
		_bInset = YES;
		_bAroundRect = YES;
		_progRadius = 0; 
		_progInset = 0;
		[self setTintColor: [UIColor colorWithRed: 43.0/255.0 green: 134.0/255.0 blue: 225.0/255.0 alpha: 1]];
		
		_bgColor = [self.backgroundColor retain];
		self.backgroundColor = [UIColor clearColor];
	}
	return self;
}

- (id) initWithProgressViewStyle: (UIProgressViewStyle) style
{
	if(self=[super initWithProgressViewStyle: style])
	{
		_cornerRadius = 5;
		_bLinearGradient = YES;
		_bBorder = YES;
		_bInset = YES;
		_bAroundRect = YES;
		_progRadius = 0;
		_progInset= 0;
		[self setTintColor: [UIColor colorWithRed: 43.0/255.0 green: 134.0/255.0 blue: 225.0/255.0 alpha: 1]];
		
		_bgColor = [self.backgroundColor retain];
		self.backgroundColor = [UIColor clearColor];
	}
	
	return self;
}

- (void)drawRect:(CGRect)rect
{	
	if([self progressViewStyle] == UIProgressViewStyleDefault)
	{
		CGContextRef ctx = UIGraphicsGetCurrentContext();
		
		CGRect contentRect = rect;
		
		if (_bInset) {
			contentRect = CGRectInset(contentRect, 1, 1);
		}
		
		
		if (_borderColor)
		{
			CGRect borderRect = contentRect;
			// border color
			addRoundedRectToPath(ctx, borderRect, _cornerRadius, _cornerRadius);
			CGContextClip(ctx);
			CGContextSetFillColorWithColor(ctx, self.borderColor.CGColor);
			CGContextFillRect(ctx, borderRect);
			
			if (_bBorder) {
				contentRect.size.width -= 1.25;
				contentRect.origin.x += 0.625;
				contentRect.size.height -= 1.25;
				contentRect.origin.y += 0.625;
			}
		}
		
		// background color
		addRoundedRectToPath(ctx, contentRect, _cornerRadius, _cornerRadius);
		CGContextClip(ctx);
		CGContextSetFillColorWithColor(ctx, _bgColor.CGColor);
		CGContextFillRect(ctx, contentRect);
		
		//draw lower gray line
		//		CGContextSetRGBStrokeColor(ctx, 178.0/255.0, 178.0/255.0, 178.0/255.0, 0.9);
		//		CGContextSetLineWidth(ctx, 2);
		//		CGContextMoveToPoint(ctx, 2.2, rect.size.height);
		//		CGContextAddLineToPoint(ctx, rect.size.width - 2.2, rect.size.height);
		//		CGContextStrokePath(ctx);
		
		//fill upperhalf with light grey
		CGRect upperhalf = contentRect;
		upperhalf.size.height /= 1.75;
		upperhalf.origin.y = 0;
		
		//		CGContextSetRGBFillColor(ctx, 202.0/255.0, 202.0/255.0, 202.0/255.0, 0.9);
		//		CGContextFillRect(ctx, upperhalf);
		
		//fill a part of the upper half with a somewhat darker grey
		//		CGRect upperhalfTop = upperhalf;
		//		upperhalfTop.size.height /= 2.7;
		//		CGContextSetRGBFillColor(ctx, 163.0/255.0, 163.0/255.0, 163.0/255.0, 0.8);
		//		CGContextFillRect(ctx, upperhalfTop);
		
		//fill the progress part with our tintcolor
		//if(_tintColor == nil)
		//	_tintColor = [UIColor colorWithRed: 43.0/255.0 green: 134.0/255.0 blue: 225.0/255.0 alpha: 1];
		
		CGRect progressRect = contentRect;
		progressRect.size.width *= [self progress];
		
		progressRect = CGRectInset(progressRect, _progInset, _progInset);
		
		addRoundedRectToPath(ctx, progressRect, _progRadius, _progRadius);
		CGContextClip(ctx);
		
		CGContextSetFillColorWithColor(ctx, [_tintColor CGColor]);
		CGContextFillRect(ctx, progressRect);
		
		if (_bAroundRect) {
			progressRect.size.width -= 1.25;
			progressRect.origin.x += 0.625;
			progressRect.size.height -= 1.25;
			progressRect.origin.y += 0.625;
			addRoundedRectToPath(ctx, progressRect, _progRadius, _progRadius);
			CGContextClip(ctx);
			CGContextSetLineWidth(ctx, 1);
			CGContextSetRGBStrokeColor(ctx, 20.0/255.0, 20.0/255.0, 20.0/255.0, 0.6);
			CGContextStrokeRect(ctx, progressRect);
		}
		
		if (_bLinearGradient)
		{
			//draw white linear gradient over upperhalf
			CGFloat colors[8] = {
				1, 1, 1, 0.45,
				1, 1, 1, 0.75
			};
			
			fillRectWithLinearGradient(ctx, upperhalf, colors, 2, nil);
		}
	}
	else
	{
		[super drawRect: rect];
	}
}

- (void) setTintColor: (UIColor *) aColor
{
	[_tintColor release];
	_tintColor = [aColor retain];
}

- (void) moveProgress
{
    if (self.progress < _targetProgress)
	{
        self.progress = MIN(self.progress + 0.01, _targetProgress);
    }
    else if(self.progress > _targetProgress)
    {
        self.progress = MAX(self.progress - 0.01, _targetProgress);
    }
    else
	{
        [_progressTimer invalidate];
        _progressTimer = nil;
    }
}

- (void) setProgress:(CGFloat)newProgress animated:(BOOL)animated
{
    if (animated)
	{
        _targetProgress = newProgress;
        if (_progressTimer == nil)
		{
            _progressTimer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(moveProgress) userInfo:nil repeats:YES];
        }
    }
	else
	{
        self.progress = newProgress;
    }
}

- (void) dealloc
{
    [super dealloc];
	[_bgColor release];
	[_tintColor release];
	[_borderColor release];
}


@end
